#ifndef NTPTIME_H
#define NTPTIME_H

#include <Arduino.h>
#include <WiFi.h>
#include <WiFiUdp.h>

class NTPTime {
   public:
    NTPTime();
    bool begin();
    bool updateTime();
    // 尝试在必要时同步时间，使用 lastNTPAttempt 来避免频繁尝试
    // 返回 true 如果尝试并成功同步；false 表示没有成功（包括未尝试）
    bool attemptSyncIfNeeded(unsigned long &lastNTPAttempt, unsigned long intervalMs = 30000);
    uint8_t getHour();
    uint8_t getMinute();
    uint8_t getSecond();
    String getFormattedTime();
    bool isTimeValid();
    void setTimezone(int offset);  // 设置时区偏移（小时）
    // Get broken-down time for an arbitrary timezone offset (hours). Returns true if time valid.
    // tzOffsetHours: requested timezone offset in hours. Outputs hour,minute,second,wday,year,month,day
    bool getTimeForOffset(int tzOffsetHours, uint8_t &hour, uint8_t &minute, uint8_t &second, uint8_t &wday, int &year, int &month, int &day);
    // DST-aware Pacific Time helper (returns Pacific time with DST rules applied)
    bool getPacificTime(uint8_t &hour, uint8_t &minute, uint8_t &second, uint8_t &wday, int &year, int &month, int &day);
    // Get Pacific timezone abbreviation ("PST" or "PDT")
    const char* getPacificTimeZoneAbbrev();

   private:
    unsigned long _lastUpdate;
    time_t _lastEpoch; // last epoch seconds from NTP
    uint8_t _hour, _minute, _second;
    bool _timeValid;
    int _timezoneOffset;  // 时区偏移（小时）
};

#endif